package com.guo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

/**
 * @Author ghg
 * @Date 2025/09/25 16:33
 * @Version 1.0
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig {

	@Bean
	public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
		http
				.authorizeHttpRequests(authorize -> authorize
						.requestMatchers("/", "/login**", "/webjars/**").permitAll() // 放行登录相关路径
						.anyRequest().authenticated() // 其他所有请求需要认证
				)
				.oauth2Login(oauth2 -> oauth2 // 启用OAuth2登录
						.loginPage("/login") // 自定义登录页路径（可选）
						.defaultSuccessUrl("/home", true) // 登录成功后的默认跳转页面
				)
				.logout(logout -> logout
						.logoutSuccessUrl("/").permitAll() // 登出成功后跳转
				);
		return http.build();
	}
}
